﻿Namespace B1Data
    ''' <summary>
    ''' 关联关系
    ''' 应用主键更改，其关联表字段跟着更改
    ''' </summary>
    ''' <remarks></remarks>
    Public Class Dependents
        Implements B1Data.IDependents
        Private _List As List(Of Dependent)
        Private _Column As B1Data.B1DataColumn

        Sub New(ByVal Column As B1Data.B1DataColumn)
            _Column = Column
        End Sub

        Public Sub Add(ByVal table As B1Data.B1DataTable, ByVal column As B1Data.B1DataColumn)
            Dim dep As Dependent
            dep = New Dependent(column.Table, column)
            Add(dep)
        End Sub

        Public Sub Add(ByVal Dependent As B1Data.Dependent)
            _List.Add(Dependent)
        End Sub

        Public Sub Remove(ByVal i As Integer)
            _List.RemoveAt(i)
        End Sub

        Public Sub Remove(ByVal Dependent As B1Data.Dependent)
            _List.Remove(Dependent)
        End Sub

        Public Sub Remove(ByVal table As B1Data.B1DataTable, ByVal column As B1Data.B1DataColumn)
            For Each deps As Dependent In _List
                If deps.Table.Equals(table) Then
                    If deps.Column.Equals(column) Then
                        _List.Remove(deps)
                    End If
                End If
            Next
        End Sub

        Public Sub GO(ByVal MasterRow As B1Data.B1DataRow)
            Dim rows() As B1Data.B1DataRow
            Dim row As B1Data.B1DataRow
            Dim MasterValue As Object

            MasterValue = MasterRow.Item(_Column)

            For Each deps As Dependent In _List
                rows = deps.Table.Select(deps.Column.ColumnName & " = " & MasterValue)
                For Each row In rows
                    row.Item(deps.Column) = MasterRow
                Next
            Next
        End Sub

    End Class

    Public Class Dependent
        Implements B1Data.IDependent

        Private _Table As B1Data.B1DataTable
        Private _Column As B1Data.B1DataColumn

        Public ReadOnly Property Table As B1Data.B1DataTable
            Get
                Return _Table
            End Get
        End Property

        Public ReadOnly Property Column As B1Data.B1DataColumn
            Get
                Return _Column
            End Get
        End Property

        Public Sub New(ByVal Table As B1Data.B1DataTable, ByVal Column As B1Data.B1DataColumn)
            _Table = Table
            _Column = Column
        End Sub



    End Class
End Namespace

